package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

public class GravitySkill extends BaseSkill {

    public GravitySkill(int level) {
        super(level);
    }

    public GravitySkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public GravitySkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(200);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("重力技能，每隔（%d-技能等级*5）将生物向上抛出。", skillConfig.getCooldown());
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown() - getSkillLevel() * 5L);
            LivingEntity target = mobEntity.getTarget();
            if (target != null && target instanceof ServerPlayerEntity serverTarget)
                serverTarget.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(target.getEntityId(), new Vec3d(0, getSkillLevel() * 0.5F, 0)));

        }
        return true;
    }
}
